$(function() {

	$('#div1').mousedown(function(ev) {
		var disX = ev.pageX - $(this).offset().left;
		var disY = ev.pageY - $(this).offset().top;

		$(document).mousemove(function(ev) {
			var L = ev.pageX - disX;
			var T = ev.pageY - disY;
			var lMin = 50;
			var lMax = $(this).innerWidth() - $('#div1').innerWidth() - 50;
			var tMin = 50;
			var tMax = $(this).innerHeight() - $('#div1').innerHeight() - 50;
			if(L < lMin) L = 0;
			if(L > lMax) L = lMax + 50;
			if(T < tMin) T = 0;
			if(T > tMax) T = tMax + 50;
			$('#div1').css('left', L).css('top', T);
		});

		$(document).mouseup(function() {
			$(document).off();
		});

		return false;
	});
});


/*
   window.onload = function() {

   oDiv1 = document.getElementById('div1');
   oDiv2 = document.getElementById('div2');

   drag(oDiv1);
   drag(oDiv2);

   };

   var oDiv1 = null;
   var oDiv2 = null;

   function drag(obj) {

   obj.onmousedown = function(ev) {
   ev = ev || event;

   if(ev.setCapture) {
   ev.setCapture();
   }

   var disX = ev.clientX - this.offsetLeft;
   var disY = ev.clientY - this.offsetTop;

   document.onmousemove = function(ev) {
   ev = ev || event;

   var L = ev.clientX - disX;
   var T = ev.clientY - disY;

   var lMin = 50;
   var lMax = document.documentElement.clientWidth - obj.offsetWidth - 
   50;
   var tMin = 50;
   var tMax = document.documentElement.clientHeight - obj.offsetHeight - 
   50;

   if(L < lMin) L = 0;
   if(L > lMax) L = lMax + 50;
   if(T < tMin) T = 0;
   if(T > tMax) T = tMax + 50;

   obj.style.left = L + 'px';
   obj.style.top = T + 'px';

   };

   document.onmouseup = function() {
   document.onmousemove = document.onmouseup = null;
   if(obj.releaseCapture) {
   ev.releaseCapture();
   }
   };
   return false;
   };
   }
   */
